package com.yunfeng.supercall.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.ly.quickdev.library.activity.DevBaseListActivity;
import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.ly.quickdev.library.utils.JsonUtils;
import com.ly.quickdev.library.view.SquareImageView;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.FriendShareListActivity.GridImgAdapter;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.FriendsShareBean;
import com.yunfeng.supercall.bean.FriendsShareCommentBean;
import com.yunfeng.supercall.bean.FriendsShareZanBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

public class FriendsShareDetailActivity extends
		DevBaseListActivity<FriendsShareCommentBean> {

	private FriendsShareBean mFriendsShare;
	private HttpClient mHttpClient = new HttpClient();
	private EditText mEditText;
	private RecyclerView mRecyclerView;

	@Override
	public void setContentView(int layoutResID) {
		// TODO Auto-generated method stub
		super.setContentView(R.layout.activity_friend_share_detail);
	}

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		mFriendsShare = getIntent().getParcelableExtra(Constants.KEY_DATA);
		mEditText = findView(R.id.et);
		FriendsShareBean friendsShare = mFriendsShare;
		View convertView = getLayoutInflater().inflate(
				R.layout.list_header_friendshare_comment, null);
		mListView.getRefreshableView().addHeaderView(convertView, null, false);
		mListView.getRefreshableView().setDrawSelectorOnTop(true);
		final ImageView avatar = ViewHolder.get(convertView, R.id.avatar);
		final TextView content = ViewHolder.get(convertView, R.id.content);
		final TextView name = ViewHolder.get(convertView, R.id.user_name);
		final TextView time = ViewHolder.get(convertView, R.id.date);
		content.setText(friendsShare.getContent());
		AppContext.displayImgWithDefault(avatar, friendsShare.getPhoto());
		name.setText(friendsShare.getNick_name());
		time.setText(friendsShare.getCreatetime());
		GridView gridView = ViewHolder.get(convertView, R.id.gridview);
		gridView.setAdapter(new GridImgAdapter(getActivity(), friendsShare
				.getPhotoList()));
		convertView.findViewById(R.id.zan).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						zanThis();
					}
				});
		mRecyclerView = (RecyclerView) convertView
				.findViewById(R.id.recycler_view);
		LinearLayoutManager manager = new LinearLayoutManager(getActivity());
		manager.setOrientation(RecyclerView.HORIZONTAL);
		mRecyclerView.setLayoutManager(manager);
		loadZanedUser();
	}

	private void zanThis() {
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_shareid", mFriendsShare.getShareid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(Constants.FUNCTION_ADD_SHARE_ZAN, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showToast(msg);
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						showToast(R.string.zan_success);
						loadZanedUser();
					}
				});
	}

	private void loadZanedUser() {
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_shareid", mFriendsShare.getShareid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(Constants.FUNCTION_GET_SHARE_ZAN_LIST, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showToast(msg);
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						mRecyclerView.setAdapter(new ZanAdapter(getActivity(),
								JsonUtils
										.parseList(data, FriendsShareZanBean.class)));
					}
				});
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mList == null || mList.isEmpty()) {
			return 1;
		}
		return super.getCount();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (mList == null || mList.isEmpty()) {
			return 0;
		}
		return 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (mList == null || mList.isEmpty()) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						android.R.layout.simple_list_item_1, parent, false);
			}
			return convertView;
		}
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_comment, parent, false);
		}
		View icon = ViewHolder.get(convertView, R.id.icon);
		if (position == 0) {
			icon.setVisibility(View.VISIBLE);
		} else {
			icon.setVisibility(View.INVISIBLE);
		}
		FriendsShareCommentBean comment = mList.get(position);
		TextView name = ViewHolder.get(convertView, R.id.user_name);
		TextView content = ViewHolder.get(convertView, R.id.content);
		TextView date = ViewHolder.get(convertView, R.id.date);
		date.setText(comment.getCreatetime());
		AppContext.displayImgWithDefault1(
				(ImageView) ViewHolder.get(convertView, R.id.avatar),
				comment.getPhoto());
		name.setText(comment.getNick_name());
		content.setText(comment.getContent());
		return convertView;
	}

	@Override
	public Mode getRefreshMode() {
		// TODO Auto-generated method stub
		return Mode.PULL_FROM_START;
	}

	public void comment(View v) {
		String content = mEditText.getText().toString();
		if (TextUtils.isEmpty(content)) {
			showToast(R.string.input_content);
			return;
		}
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_content", content);
			json.put("i_shareid", mFriendsShare.getShareid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(Constants.FUNCTION_ADD_SHARE_COMMENT, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showToast(msg);
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						mEditText.setText("");
						loadData();
					}
				});
	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_shareid", mFriendsShare.getShareid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(Constants.FUNCTION_GET_SHARE_COMMENT_LIST, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showList();
						mListView.onRefreshComplete();
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						mList = JsonUtils.parseList(data,
								FriendsShareCommentBean.class);
						setListAdapter();
					}
				});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	public static class MyViewHolder extends
			android.support.v7.widget.RecyclerView.ViewHolder {

		public MyViewHolder(View arg0) {
			super(arg0);
			// TODO Auto-generated constructor stub
		}

	}

	public class ZanAdapter extends Adapter<MyViewHolder> {

		private List<FriendsShareZanBean> mFriendsShareZans;

		public ZanAdapter(Context context, List<FriendsShareZanBean> list) {
			mFriendsShareZans = list;
		}

		@Override
		public int getItemCount() {
			// TODO Auto-generated method stub
			return mFriendsShareZans.size();
		}

		@Override
		public void onBindViewHolder(MyViewHolder arg0, int arg1) {
			// TODO Auto-generated method stub
			ImageView iv = (ImageView) arg0.itemView;
			LayoutParams mLayoutParams = new LayoutParams(getResources()
					.getDimensionPixelSize(R.dimen.avatar_chat_size),
					getResources().getDimensionPixelSize(
							R.dimen.avatar_chat_size));
			iv.setLayoutParams(mLayoutParams);
			AppContext.displayImgWithDefault1(iv, mFriendsShareZans.get(arg1)
					.getPhoto());
		}

		@Override
		public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
			// TODO Auto-generated method stub
			SquareImageView imageView = new SquareImageView(getActivity());
			imageView.setPadding(8, 8, 8, 8);
			MyViewHolder holder = new MyViewHolder(imageView);
			return holder;
		}

	}

}
